package edu.hubu.talentmanagementsystem.gen;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;

/**
 * @author moonlan
 * date 2021/3/9 下午1:13
 */
@SuppressWarnings(value = "unused")
public class ContextGen {
    public static void main(String[] args) {
        String path = "/home/moonlan/code/java/talent-management-system/src/main/resources/ftls/";
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_29);
        String genPath = "/home/moonlan/code/java/talent-management-system/src/main/java/edu/hubu/talentmanagementsystem/mapper/context/";
        File file = new File(genPath);
        if (!file.exists()) {
            file.mkdir();
        }
        try {
            configuration.setDirectoryForTemplateLoading(new File(path));
            List<Class<?>> classes = GenTool.getClasses("edu.hubu.talentmanagementsystem.mapper");
            HashMap<String, Object> map = new HashMap<>();
            HashMap<String, String> mappers = new HashMap<>();
            for (Class<?> clazz : classes) {
                mappers.put(clazz.getSimpleName(), clazz.getSimpleName().replace(clazz.getSimpleName().charAt(0),
                        (char) (clazz.getSimpleName().charAt(0) + 32)));
            }
            map.put("mappers", mappers);
            Template template = configuration.getTemplate("context.flt");
            FileWriter writer =
                    new FileWriter(genPath + "MapperContext.java");
            template.process(map, writer);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
